/**
 Brainfuck-Robots Bluetooth App
 Copyright (C) <2014>  <Simon Grafenhorst>

 This file is part of the Brainfuck-Robots Bluetooth App.

 The Brainfuck-Robots Bluetooth App is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 The Brainfuck-Robots Bluetooth App is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with the Brainfuck-Robots Bluetooth App.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.brainfuckrobots.bluetooth;

import android.app.AlertDialog;
import android.app.Dialog;
import android.bluetooth.BluetoothDevice;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;

import java.util.ArrayList;

public class ChooseDeviceDialog extends DialogFragment {
    private int selectedItem = 0;
    private AlertDialog dialog;
    private ArrayList<BluetoothDevice> devices;
    public String tag = "ChooseDeviceDialog";

    public void setDevices(ArrayList<BluetoothDevice> devices) {
        this.devices = devices;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        CharSequence[] deviceSequence;
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        if (devices != null && devices.size() > 0) {
            deviceSequence = new CharSequence[devices.size()];
            int i = 0;
            for (BluetoothDevice device : devices) {
                deviceSequence[i] = device.getName() + " " + device.getAddress();
                i++;
            }
            builder.setSingleChoiceItems(deviceSequence, selectedItem, new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    selectedItem = which;
                }
            });

        }
        builder.setCustomTitle(MainActivity.getContext().getLayoutInflater().inflate(R.layout.chooser_dialog, null));
        dialog = builder.setPositiveButton(R.string.connect, new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (!devices.isEmpty()) {
                    ((MainActivity) getActivity()).connect(devices.get(selectedItem));
                }
            }
        })
                .setNegativeButton(R.string.cancel, new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        selectedItem  = 0;
                    }
                }).create();
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                if(devices == null || devices.isEmpty()){
                    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
                }
            }
        });
        return dialog;
    }
}